#include "SypEngine.h"

#ifdef CHECKMEM
#include "mmgr.h"
#endif

namespace Syp
{

	Frame* Frame::createFrame(const String& name, float Width, float Height)
	{
		return (new Frame(name,Width,Height));
	}

	Frame::Frame(const String& name,float Width, float Height):GuiBase(name,Width,Height,false),beingMoved(false)
	{
		genMesh();
		title = Label::createLabel(name+"Label",name);
		title->getMIRef().setPosition( mi->getPosition().x,mi->getPosition().y + height - 20.0f,mi->getPosition().z + 1.0f);
		title->getMIRef().bindToNode(mi);
		

	}

	Label* Frame::getTitle()
	{
		return title;
	}

	void Frame::setTitleBarColor(Syp::Color3f& color)
	{
		FloatArray& data = mesh->getDataRef();
		data[35] = color.r;
		data[36] = color.g;
		data[37] = color.b;

		data[43] = color.r;
		data[44] = color.g;
		data[45] = color.b;

		data[51] = color.r;
		data[52] = color.g;
		data[53] = color.b;

		data[59] = color.r;
		data[60] = color.g;
		data[61] = color.b;


		mesh->updateMesh();

	}	

	void Frame::setTitleBarColor(Syp::Color3f &color_bl, Syp::Color3f &color_br, Syp::Color3f &color_tr, Syp::Color3f& color_tl)
	{
		FloatArray& data = mesh->getDataRef();
		data[35] = color_bl.r;
		data[36] = color_bl.g;
		data[37] = color_bl.b;

		data[43] = color_br.r;
		data[44] = color_br.g;
		data[45] = color_br.b;

		data[51] = color_tr.r;
		data[52] = color_tr.g;
		data[53] = color_tr.b;

		data[59] = color_tl.r;
		data[60] = color_tl.g;
		data[61] = color_tl.b;

		mesh->updateMesh();

	}

	void Frame::mouseLeftDown()
	{
		GuiBase::mouseLeftDown();
		const MouseState& ms = InputManager::getInputManager().getMouseStateRef();
		if(ms.mouseLeft && ms.mouseY > mi->getPosition().y + height - 20.0f && ms.mouseY < mi->getPosition().y + height)
		{
			beingMoved = true;
		}
	}

	void Frame::mouseLeftUp()
	{
		GuiBase::mouseLeftUp();
		const MouseState& ms = InputManager::getInputManager().getMouseStateRef();
		if(!(ms.mouseLeft && ms.mouseY > mi->getPosition().y + height - 20.0f && ms.mouseY < mi->getPosition().y + height))
		{
			beingMoved = false;
		}

	}
//
	void Frame::mouseMove(const MouseState& ms)
	{
		if(beingMoved)
		{//inside title
			mi->offsetPosition(static_cast<float>(ms.mouseX - ms.lastMouseX), static_cast<float>(ms.mouseY - ms.lastMouseY),0.0f);
		}
	}

	

	void Frame::genMesh()
	{
		mat = Material::createMaterial(name + " Material");
		SubMaterial sm;
		sm.btype = NOBLEND;
		//we dont want the GUI to be affected by the lights
		sm.lit = false;
		sm.diffuse[0] = 1.0f;
		//sm.useTexture(TextureObj::loadTexture("Textures/font.png",false));
		//sm.useTexture(TextureObj::loadTexture(fileName,false));
		sm.useEffect("GuiFF");
		mat->subMaterials.push_back(sm);			

		mesh = NIndexedMesh::createNIndexedMesh(name + " Mesh", DYNAMIC);
		mesh->setVertexType(VC3T);
		mesh->setRenderingFormat(FORMAT_QUADS);
		IndexBlock ib;
		ib.materialID = 0;
		mesh->getDrawCallsRef().push_back(ib);
		mesh->getNumVerticesRef() = 8;

		mi = MeshInstance::createMeshInstance();
		mi->mode = ORTHO;
		mi->useMesh(mesh);
		mi->useMaterial(mat);



		FloatArray& data = mesh->getDataRef();
		data.resize(64);

		data[0] = 0.0f;
		data[1] = 0.0f;
		data[2] = 0.0f;
		data[3] = 0.3f;//Color
		data[4] = 0.3f;
		data[5] = 0.3f;
		data[6] = 0.0f;//UV
		data[7] = 0.0f;

		data[8] = width;
		data[9] = 0.0f;
		data[10] = 0.0f;
		data[11] = 0.7f;
		data[12] = 0.7f;
		data[13] = 0.7f;
		data[14] = 1.0f;//UV
		data[15] = 0.0f;

		data[16] = width;
		data[17] = height - 20.0f;
		data[18] = 0.0f;
		data[19] = 1.0f;
		data[20] = 1.0f;
		data[21] = 1.0f;
		data[22] = 1.0f;//UV
		data[23] = 1.0f;

		data[24] = 0.0f;
		data[25] = height - 20.0f;
		data[26] = 0.0f;
		data[27] = 0.7f;
		data[28] = 0.7f;
		data[29] = 0.7f;
		data[30] = 0.0f;//UV
		data[31] = 1.0f;

		//TitleBar
		data[32] = 0.0f;
		data[33] = height - 20.0f;
		data[34] = 0.0f;
		data[35] = 0.8f;
		data[36] = 0.8f;
		data[37] = 0.0f;
		data[38] = 0.0f;//UV
		data[39] = 0.0f;

		data[40] = width;
		data[41] = height - 20.0f;
		data[42] = 0.0f;
		data[43] = 0.8f;
		data[44] = 0.8f;
		data[45] = 0.0f;
		data[46] = 0.0f;//UV
		data[47] = 0.0f;

		data[48] = width;
		data[49] = height;
		data[50] = 0.0f;
		data[51] = 0.8f;
		data[52] = 0.8f;
		data[53] = 0.0f;
		data[54] = 0.0f;//UV
		data[55] = 0.0f;

		data[56] = 0.0f;
		data[57] = height;
		data[58] = 0.0f;
		data[59] = 0.8f;
		data[60] = 0.8f;
		data[61] = 0.0f;
		data[62] = 0.0f;//UV
		data[63] = 0.0f;



		//data[0] = 0.0f;
		//data[1] = 0.0f;
		//data[2] = 0.0f;
		//data[3] = 0.0f;
		//data[4] = 0.0f;

		//data[5] = width;
		//data[6] = 0.0f;
		//data[7] = 0.0f;
		//data[8] = 1.0f;
		//data[9] = 0.0f;

		//data[10] = width;
		//data[11] = height - 10.0f;
		//data[12] = 0.0f;
		//data[13] = 1.0f;
		//data[14] = 1.0f;

		//data[15] = 0.0f;
		//data[16] = height - 10.0f;
		//data[17] = 0.0f;
		//data[18] = 0.0f;
		//data[19] = 1.0f;

		////TitleBar
		//data[20] = 0.0f;
		//data[21] = height - 10.0f;
		//data[22] = 0.0f;
		//data[23] = 0.0f;
		//data[24] = 0.0f;

		//data[25] = width;
		//data[26] = height - 10.0f;
		//data[27] = 0.0f;
		//data[28] = 1.0f;
		//data[29] = 0.0f;

		//data[30] = width;
		//data[31] = height;
		//data[32] = 0.0f;
		//data[33] = 1.0f;
		//data[34] = 1.0f;

		//data[35] = 0.0f;
		//data[36] = height;
		//data[37] = 0.0f;
		//data[38] = 0.0f;
		//data[39] = 1.0f;


		mesh->updateMesh();


	}

	Frame::~Frame()
	{
		GuiBase::destroyGuiComponent(title);
	}

}
